home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / AppleShare IP SDK / ASIP User Authentication Module / Sample Code / Client UAM / SampleUAM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-17  |  4.4 KB  |  200 lines  |  [TEXT/CWIE]

  1. // SampleUAM.c
  2.  
  3. #include <String.h>
  4. #include <Resources.h>
  5. #include <A4Stuff.h>
  6.  
  7. #include "SampleUAM.h"
  8. #include "AFPPackets.h"
  9.  
  10. unsigned char    commandBuffer[200];
  11. unsigned char    replyBuffer[512];
  12. StringPtr        gAFPVersion;
  13.  
  14.  
  15.  
  16. StringPtr    FigureAFPVersion(AFPSrvrInfo *,ClientUAMCallbackRec *theCallbacks);
  17.  
  18. //pascal    OSErr    UAMCall(UAMArgs *theArgs);
  19. pascal    OSErr    main(UAMArgs *theArgs)
  20. {
  21.     EnterCodeResource();
  22.     OSErr    error;
  23.     switch(theArgs->command)
  24.     {
  25.         case    kUAMOpen:
  26.             error =    SampleOpen(theArgs);
  27.             break;
  28.                                 
  29.         case    kUAMPWDlog:
  30.             error =    kNotForUs;
  31.             break;
  32.  
  33.         case    kUAMLogin:
  34.             error =    SampleLogin(theArgs);
  35.             break;
  36.         
  37.         case    kUAMVSDlog:
  38.             DebugStr("\pPut up a Volume Select dialog");
  39.             error =    noErr;
  40.             break;
  41.  
  42.         case    kUAMChgPassDlg:
  43.             error =    kNotForUs;
  44.             break;
  45.  
  46.         case    kUAMChgPass:
  47.             error =    kNotForUs;
  48.             break;
  49.  
  50.         case    kUAMGetInfoSize:
  51.             error =    kNotForUs;
  52.             break;
  53.  
  54.         case    kUAMGetInfo:
  55.             error =    kNotForUs;
  56.             break;
  57.  
  58.         case    kUAMClose:
  59.             error =    noErr;
  60.             break;
  61.  
  62.         default:
  63.             error =    kNotForUs;
  64.             break;
  65.     }
  66.     ExitCodeResource();
  67.     return error;
  68. }
  69.  
  70.  
  71. long    SampleOpen(UAMArgs *theArgs)
  72.                         
  73. {
  74.     gAFPVersion = FigureAFPVersion(theArgs->Opt.open.srvrInfo,theArgs->callbacks);
  75.     
  76.     theArgs->result = kSampleCfg;
  77.     return noErr;
  78. }
  79.  
  80.  
  81. OSStatus    SampleLogin(UAMArgs *theArgs){
  82.     OSStatus        theError = kUAMError;
  83.     Ptr                cmd;
  84.     unsigned long    cmdSize;
  85.     Handle            theUAMName;
  86.     UAMMessage        message;
  87.     StringPtr        user = theArgs->Opt.auth.userName;
  88.     StringPtr        password = theArgs->Opt.auth.password;
  89.     
  90.     //DebugStr("\pin SampleLogin");
  91.  
  92.     if(!gAFPVersion){
  93.         // put up an alert & return userCancelled error
  94.         DebugStr("\pno AFP version");
  95.         return userCanceledErr;
  96.     }
  97.     if(theArgs->callbacks)
  98.     {
  99.         commandBuffer[0] = kFPLogin;
  100.         cmd = (Ptr) &commandBuffer[1];
  101.         memcpy(cmd,(const char *)&gAFPVersion[0],gAFPVersion[0]+1);
  102.         cmd += gAFPVersion[0] + 1;
  103.         
  104.         // get the UAMSTring from the resource
  105.         theUAMName = Get1Resource(kUAMStr,kUAMProtoName);
  106.         if(!theUAMName)
  107.             return ResError();    // really should be something else (this will be wrong if ResLoad is false)
  108.  
  109.         // put it into the command buffer    
  110.         HLock(theUAMName);
  111.         memcpy(cmd,(const char *)&((*theUAMName)[0]),(*theUAMName)[0]+1);
  112.         cmd += (*theUAMName)[0]+1;
  113.         HUnlock(theUAMName);
  114.         ReleaseResource(theUAMName);
  115.         
  116.         // copy in the username
  117.         memcpy(cmd,(const char *)&user[0],user[0]+1);
  118.         cmd += user[0]+1;
  119.         if(((UInt32)cmd - (UInt32)commandBuffer) & 0x01)    // is this an odd boundry?
  120.         {
  121.             *cmd++ = 0x00;    // put in some padding
  122.         } 
  123.         
  124.         // copy in the password, only 8 bytes of password
  125.         memcpy(cmd,(const char *)&password[0],8);
  126.         cmd += 8;
  127.         
  128.         // get the cmd buffer size
  129.         cmdSize = (unsigned long)((unsigned long)cmd - (unsigned long)commandBuffer);
  130.         
  131.         message.commandCode = kOpenSession;
  132.         message.cmdBuffer = commandBuffer;
  133.         message.cmdBufferSize = cmdSize;
  134.         message.replyBuffer = nil;
  135.         message.replyBufferSize = 0;
  136.         message.completion = nil;
  137.         message.contextPtr = nil;
  138.         
  139.         //DebugStr("\pabout to make the login call");
  140.          
  141.         theError = theArgs->callbacks->OpenSessionUPP(theArgs->Opt.auth.srvrAddress,nil,&message);
  142.         if(!theError){
  143.             theArgs->sessionRefNum = message.sessionRefNum;
  144.         }
  145.         theError = message.result;
  146.         
  147.     }
  148.     return theError;    
  149.  
  150. }
  151.  
  152.  
  153.  
  154. StringPtr    FigureAFPVersion(AFPSrvrInfo *info,ClientUAMCallbackRec *callbacks)
  155. {
  156.     struct    AFPClientInfo *theClientInfo = nil;
  157.     short    index;
  158.     Ptr        versBuf;
  159.     UInt32    versBufsize;
  160.     GetClientInfoPtr    *fcn;
  161.     
  162.     //DebugStr("\pFigureAFPVersion");
  163.     callbacks->GetClientInfoUPP(kAFPClientInfo,(ClientInfo **)&theClientInfo);
  164.  
  165.     if(theClientInfo){                
  166.     // go through the list of AFP versions supported and try to find them
  167.     // in the SrvrInfoBuffer, first match gets it 
  168.         versBuf = (Ptr)((UInt32)info + info->fVerCountOffset+1);
  169.         versBufsize = kMaxAFPCommand - info->fVerCountOffset;    // the largest size
  170.         
  171.         for(index = 0; index < theClientInfo->fNumAFPVersions; index++){
  172.             if(FindStringInBuf(theClientInfo->fAFPVersionStrs[index],versBuf,versBufsize)){
  173.                 return theClientInfo->fAFPVersionStrs[index];
  174.             }
  175.         }
  176.     }
  177.     return    nil;
  178. }
  179.  
  180. // rather clunky but it works
  181. Boolean        FindStringInBuf(StringPtr string, Ptr buf, UInt32 bufSize)
  182. {
  183.     Ptr        end = buf + bufSize;
  184.     Byte    len = string[0] + 1;
  185.     short    index;
  186.     
  187.     while((buf < end) && (*buf++ != string[0])) ;    // scan for the proper length
  188.  
  189.     if(!(buf < end)){
  190.         return false;
  191.     }
  192.     for(index = 1; (index < len) && (buf > end); index++){
  193.         if(*buf++ != string[index])
  194.             return false; 
  195.     }
  196.     if(!(buf < end)){
  197.         return false;
  198.     }
  199.     return true;
  200. }